package com.cskaoyan.service;

import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.lzp.KeywordCreatBO;
import com.cskaoyan.bean.lzp.KeywordData;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.mapper.KeywordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * \* Description:
 * \* @author: Lizhanpeng
 * \* Date: 2022/5/6
 * \
 */
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public KeywordData query(BaseParam param, String keyword, String url) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        PageHelper.startPage(page,limit);

       List<Keyword> list= keywordMapper.select(sort,order,keyword,url);
        PageInfo<Keyword> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return KeywordData.data(total,pages,limit,page,list);
    }

    @Override
    public Keyword create(KeywordCreatBO keywordCreatBO) {
        Keyword keyword = new Keyword();
        keyword.setKeyword(keywordCreatBO.getKeyword());
        keyword.setUrl(keywordCreatBO.getUrl());
        keyword.setIsDefault(keywordCreatBO.isIsDefault());
        keyword.setIsHot(keywordCreatBO.isIsHot());
        keyword.setAddTime(new Date());
        keyword.setUpdateTime(new Date());
        int i = keywordMapper.insertSelective(keyword);
        int maxId= keywordMapper.getMaxId();
        Keyword keyword1 = keywordMapper.selectByPrimaryKey(maxId);
        return keyword1;
    }

    @Override
    public Keyword update(Keyword keyword) {
        keyword.setUpdateTime(new Date());
        keywordMapper.updateByPrimaryKeySelective(keyword);
        Keyword keyword1 = keywordMapper.selectByPrimaryKey(keyword.getId());
        return keyword1;
    }

    @Override
    public void delete(Keyword keyword) {
        keywordMapper.deleteByPrimaryKey(keyword.getId());
    }
}
